package io.github.talelin.latticy.service.impl.ag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.talelin.latticy.dto.ag.monitoringdevices.CreateOrUpdateMonitoringDevicesDTO;
import io.github.talelin.latticy.mapper.ag.MonitoringDevicesMapper;
import io.github.talelin.latticy.mapper.ag.MonitoringDevicesSelectMapper;
import io.github.talelin.latticy.model.ag.MonitoringDevicesDO;
import io.github.talelin.latticy.model.ag.MonitoringDevicesDetailDO;
import io.github.talelin.latticy.service.ag.MonitoringDevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MonitoringDevicesServiceImpl implements MonitoringDevicesService {
    @Autowired
    private MonitoringDevicesMapper monitoringDevicesMapper;

    @Autowired
    private MonitoringDevicesSelectMapper selectMapper;

    @Override
    public MonitoringDevicesDetailDO getDetailById(int id) {
        return selectMapper.selectById(id);
    }

    @Override
    public MonitoringDevicesDO getById(int id) {
//        MonitoringDevicesDO md =
        return monitoringDevicesMapper.selectById(id);
    }

    @Override
    public List<MonitoringDevicesDO> getMonitoringDevices() {
        List<MonitoringDevicesDO> mdList = monitoringDevicesMapper.selectList(null);
        return mdList;
    }


    @Override
    public boolean createMonitoringDevices(CreateOrUpdateMonitoringDevicesDTO validator) {
        MonitoringDevicesDO monitoringDevice = new MonitoringDevicesDO();
        monitoringDevice.setRoomId(validator.getRoom_id());
        monitoringDevice.setMdName(validator.getMd_name());
        monitoringDevice.setBuildingName(validator.getBuilding_name());
        monitoringDevice.setMdModel(validator.getMd_model());
        monitoringDevice.setMdMaker(validator.getMd_maker());
        monitoringDevice.setMdMakerNum(validator.getMd_maker_num());
        monitoringDevice.setMdInputDate(validator.getMd_input_date());
        monitoringDevice.setMdUseDate(validator.getMd_use_date());
        monitoringDevice.setMdState(validator.getMd_state());
        return monitoringDevicesMapper.insert(monitoringDevice)>0;
    }

    @Override
    public boolean deleteMonitoringDevices(int id) {
        return monitoringDevicesMapper.deleteById(id)>0;
    }

    @Override
    public boolean updateMonitoringDevieces(MonitoringDevicesDO md,CreateOrUpdateMonitoringDevicesDTO validator){
        md.setRoomId(validator.getRoom_id());
        md.setBuildingName(validator.getBuilding_name());
        md.setMdName(validator.getMd_name());
        md.setMdModel(validator.getMd_model());
        md.setMdMaker(validator.getMd_maker());
        md.setMdMakerNum(validator.getMd_maker_num());
        md.setMdInputDate(validator.getMd_input_date());
        md.setMdUseDate(validator.getMd_use_date());
        md.setMdState(validator.getMd_state());
        return monitoringDevicesMapper.updateById(md)>0;
    }

    @Override
    public IPage<MonitoringDevicesDetailDO> getMonitoringDeveicesByPage(Page<MonitoringDevicesDetailDO> pager) {
        QueryWrapper<MonitoringDevicesDetailDO> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("update_time");
        IPage<MonitoringDevicesDetailDO> iPage =selectMapper.selectPage(pager, wrapper);
        return iPage;
    }

    @Override
    public IPage<MonitoringDevicesDetailDO> searchRoomByKeyword(Page<MonitoringDevicesDetailDO> pager, String keyword) {
        QueryWrapper<MonitoringDevicesDetailDO> wrapper = new QueryWrapper<>();
        wrapper.like("md_name",keyword).or()
                .like("building_name", keyword).or()
                .like("room_name", keyword).or()
                .like("md_model", keyword).or()
                .like("md_maker", keyword).or()
                .like("md_maker_num", keyword).or()
                .like("md_state", keyword).or();
        IPage<MonitoringDevicesDetailDO> iPage = selectMapper.selectPage(pager, wrapper);
        return iPage;
    }
}
